package com.tongji.housediy.view
{
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.view.components.AbstractDrawingRoom;
	import com.tongji.housediy.view.components.GridContainer;
	import com.tongji.housediy.view.components.IndependentFloor;
	import com.tongji.housediy.view.titlewindows.WaitingOnlineTitleWindow;
	import com.tongji.housediy.vo.ExchangeData;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.core.UIComponent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class TopMenuCompMediator extends Mediator
	{
		[Inject]
		public var view:TopMenuComp;
		
		[Inject]
		public var roomModel:ExchangeDataModel;
		
		override public function onRegister():void
		{
			view.exportPic.addEventListener(MouseEvent.CLICK, exportPicHandler);
			view.productBtn.addEventListener(MouseEvent.CLICK, waitingOnlineHandler);
			view.lightBtn.addEventListener(MouseEvent.CLICK, waitingOnlineHandler);
			view.budgetBtn.addEventListener(MouseEvent.CLICK, waitingOnlineHandler);
		}
		
		public function waitingOnlineHandler(event:MouseEvent):void
		{
			var waitingOnlineTitleWindow:WaitingOnlineTitleWindow = new WaitingOnlineTitleWindow();
			PopUpManager.addPopUp(waitingOnlineTitleWindow, view.parent, true);
			PopUpManager.centerPopUp(waitingOnlineTitleWindow);
			mediatorMap.createMediator(waitingOnlineTitleWindow);
		}
		
		public function exportPicHandler(event:MouseEvent):void
		{
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			var roomCount:int = roomModel.gridContainer.numChildren;
			if (roomCount > 0)
			{
				var minX:Number = 10000;
				var minY:Number = 10000;
				var maxX:Number = -1;
				var maxY:Number = -1;
				var target:UIComponent = roomModel.gridContainer;
				var boundingBox:Rectangle;
				for (var i:int = 0; i < roomModel.gridContainer.numChildren; i++)
				{
					if ((roomModel.gridContainer.getChildAt(i) is AbstractDrawingRoom) && !(roomModel.gridContainer.getChildAt(i) is IndependentFloor))
					{
						var host:AbstractDrawingRoom = roomModel.gridContainer.getChildAt(i) as AbstractDrawingRoom;
						boundingBox = host.getRect(host.parent);
						if (boundingBox.x <= minX)
							minX = boundingBox.x;
						if (boundingBox.y <= minY)
							minY = boundingBox.y;
						if (boundingBox.x + boundingBox.width >= maxX)
							maxX = boundingBox.x + boundingBox.width;
						if (boundingBox.y + boundingBox.height >= maxY)
							maxY = boundingBox.y + boundingBox.height;
					}					
				}
				var matrix:Matrix = new Matrix();
				matrix.translate(-minX + 32, -minY + 32);
				var bitmapData:BitmapData = new BitmapData(maxX - minX + 64, maxY - minY + 64);
				bitmapData.draw(target,matrix); 
				var jpg:JPEGEncoder = new JPEGEncoder(); 
				var jpgByteArray:ByteArray = jpg.encode(bitmapData); 
				var fr:FileReference = new FileReference(); 
				fr.save(jpgByteArray, 'export.jpg');
			}
			else
				Alert.show("请您先进行设计");	
		}
	}
}